﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace C_Sharp_Timer_Interrupt
{
    public partial class Fm_Road : Form
    {
        //创建系统时间控件


        //初始化窗体
        public Fm_Road()
        {
            InitializeComponent();
            Ti_System.Interval = 5;
        }

        //开始按钮的点击事件
        private void Bt_Start_Click(object sender, EventArgs e)
        {
            Ti_System.Start();
        }

        //加速按钮的点击事件
        private void Bt_Accelerate_Click(object sender, EventArgs e)
        {
            if (Ti_System.Interval <= 10)
            {
                Ti_System.Interval += 1;
            }
        }

        //减速按钮的点击事件
        private void Bt_Decelerate_Click(object sender, EventArgs e)
        {
            if (Ti_System.Interval >1 )
            {
                Ti_System.Interval -= 1;
            }
        }

        //暂停按钮的点击事件
        private void Bt_Stop_Click(object sender, EventArgs e)
        {
            Ti_System.Stop();
        }

        //timer组件的点击事件，对小车位置进行刷新，从而达到移动的视觉效果
        private void Ti_System_Tick(object sender, EventArgs e)
        {
            //对窗体进行更新
            this.Invalidate();
            //控件按照速度移动
            Point p = new Point();
            Point ps = new Point(0,0);
            p.X= Pb_Tank.Location.X+Ti_System.Interval * 2;
            p.Y= Pb_Tank.Location.Y+Ti_System.Interval;
            Pb_Tank.Location = p;
            if (Pb_Tank.Location.Y>=Bottom)
            {
                Pb_Tank.Location = ps;
            }
        }

        private void Fm_Road_FormClosing(object sender, FormClosingEventArgs e)
        {
            //窗体关闭原因为单击"关闭"按钮或Alt+F4  
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;           //取消关闭操作 表现为不关闭窗体  
                this.Hide();               //隐藏窗体  
            }
        }

        //"显示窗体"单击事件 
        private void 显示窗体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();                                //窗体显示  
            this.WindowState = FormWindowState.Normal;  //窗体状态默认大小  
            this.Activate();
        }
        //"隐藏窗体"单击事件  
        private void 隐藏窗体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        //"退出"单击事件
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //点击"是(YES)"退出程序  
            if (MessageBox.Show("确定要退出程序?", "安全提示",
                        System.Windows.Forms.MessageBoxButtons.YesNo,
                        System.Windows.Forms.MessageBoxIcon.Warning)
                == System.Windows.Forms.DialogResult.Yes)
            {
                No_Hide.Visible = false;   //设置图标不可见  
                this.Close();                  //关闭窗体  
                this.Dispose();                //释放资源  
                Application.Exit();            //关闭应用程序窗体   
            }
        }

        //鼠标左键图标事件
        private void No_Hide_MouseClick(object sender, MouseEventArgs e)
        {
            //点击鼠标"左键"发生  
            if (e.Button == MouseButtons.Left)
            {
                this.Visible = true;                        //窗体可见  
                this.WindowState = FormWindowState.Normal;  //窗体默认大小  
                this.No_Hide.Visible = true;            //设置图标可见  
            }
        }

        private void Fm_Road_Load(object sender, EventArgs e)
        {

        }
    }
}
